const http = require('../../../../utils/http.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        navBar_3: {
            value: '',
            btn: 'goBack',
            inputTxt: 'inputTxt'
        },
        resultList: [],
        star: [],
        nullData: {
            img: http.imgUrl + 'null_data_1.jpg',
            txt: '暂时没有相关数据'
        }
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 获取输入的关键词
     */
    inputTxt(e) {
        let that = this,
            tp = that.data.tp
            
        that.setData({
            resultList: [],
            star: [],
            navBar_3: {
                value: e.detail.value,
                btn: 'goBack',
                inputTxt: 'inputTxt'
            },
            key: e.detail.value
        })

        if (e.detail.value) {
            switch (tp) {
                case '1':
                    that.getArticleList(0)
                    break
                default:
                    that.getList(0)
            }
        }
    },

    /**
     * 获取图说文章列表
     */
    getArticleList(page) {
        let that = this,
            url = 'Article_articleList',
            key = that.data.key,
            token = wx.getStorageSync('token')

        http._post(
            url,
            { search: key, page: page, token: token },
            res => {
                // console.log(res.data)

                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        resultList: data.data.list,
                        page: data.data.page,
                        sumPage: data.data.sum_page,
                        isPage: data.data.is_page  // 是否有下一页
                    })
                } else {
                    that.setData({
                        resultList: [],
                        page: data.data.page,
                        sumPage: data.data.sum_page,
                        isPage: data.data.is_page
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 到图说详情
     */
    goDetail(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/index/picDesc/picDdetail/picDdetail?id=' + id
        })
    },

    /**
     * 获取课程列表
     */
    getList(p) {
        let that = this,
            url = 'Lesson_formList',
            form_id = that.data.form_id,
            key = that.data.key,
            token = wx.getStorageSync('token')

        http._post(
            url,
            { page: p, search: key, token: token },
            res => {
                // console.log(res.data)

                let data = res.data
                if (data.re === '1') {
                    let temp_star_arr = that.data.star,
                        temp_arr = that.data.resultList
                    for (let item of data.data.list) {
                        temp_star_arr.push(Number(item.star))
                        temp_arr.push(item)
                    }
                    that.setData({
                        resultList: temp_arr,
                        star: temp_star_arr,
                        is_page: data.data.is_page,
                        page: data.data.page,
                        sum_page: data.data.sum_page
                    })
                } else {
                    that.setData({
                        resultList: [],
                        star: [],
                        is_page: data.data.is_page,
                        page: data.data.page,
                        sum_page: data.data.sum_page
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 到课堂详情
     */
    goForumDetail(e) {
        let id = http.dataIndex(e)[1]
        wx.navigateTo({
            url: '/pages/course/course?id=' + id,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this,
            tp = options.type || '0',
            search = options.search || ''

        that.setData({
            navBar_3: {
                value: search,
                btn: 'goBack',
                inputTxt: 'inputTxt'
            },
            key: search,
            tp: tp
        })

        if (search) {
            switch (tp) {
                case '1':
                    that.getArticleList(0)
                    break
                default:
                    that.getList(0)
            }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})